package com.arges.web.serviceImpl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.dao.DocDao;
import com.arges.web.dao.GenericDao;
import com.arges.web.dao.WorkDao;
import com.arges.web.domain.bean.DocBean;
import com.arges.web.domain.pojo.Doc;
import com.arges.web.domain.pojo.Work;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.DocManager;

/**
 * 用户管理业务层
 *
 * @author zxj
 *
 */
@Service
public class DocManagerImpl extends GenericManageImpl<Doc, DocBean> implements DocManager
{
	private Logger LOGGER = LoggerFactory.getLogger(DocManagerImpl.class);

	@Autowired
	private DocDao docDao;

	@Autowired
	WorkDao workDao;

	@Override
	public GenericDao<Doc, Long> getGenericDao()
	{
		// TODO Auto-generated method stub
		return docDao;
	}

	@Override
	public DocBean getByName(String name)
	{
		DocBean docBean = null;
		Doc doc = docDao.getByName(name);
		try
		{
			if (null != doc)
			{
				docBean = new DocBean();
				BeanUtils.copyProperties(doc, docBean);
			}
		}
		catch (Exception e)
		{
			LOGGER.error("find doctor by name is error . name=" + name);
		}
		return docBean;
	}

	@Override
	public PageInfo<DocBean> getPageDoc(PageInfo<Doc> pageInfo, String userName)
	{
		PageInfo<DocBean> beanPageList = null;
		try
		{
			Criterion criterion = null;
			if (StringUtils.isNotBlank(userName.trim()))
			{
				criterion = Restrictions.like("name", userName, MatchMode.ANYWHERE);
			}
			PageInfo<Doc> pojoPageList = docDao.findAll(pageInfo, Doc.class, criterion);

			beanPageList = WebUtils.pojoToBeanPage(pojoPageList, DocBean.class);

			List<Work> list = workDao.findAll();

			for (DocBean docBean : beanPageList.getResult())
			{
				for (Work work : list)
				{
					if (docBean.getEno().equals(work.getEno()))
					{
						docBean.setD_name(work.getD_name());
					}
				}
			}
		}
		catch (Exception e)
		{
			return null;
		}
		return beanPageList;
	}

	@Override
	public DocBean findDocById(long parseLong)
	{
		Doc doc = docDao.get(parseLong);
		DocBean docBean = null;
		if (null != doc)
		{
			docBean = new DocBean();
			BeanUtils.copyProperties(doc, docBean);
		}
		Work work = workDao.getByEno(docBean.getEno());
		if (work != null)
		{
			docBean.setAge(work.getAge());
			docBean.setD_name(work.getD_name());
			docBean.setId_card(work.getId_card());
			docBean.setSex(work.getSex());
			docBean.setPhone(work.getPhone());
		}
		return docBean;
	}

	@Override
	public void deleteByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] deleteId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			deleteId[i] = Long.valueOf(split[i]);
		}
		docDao.deleteDocByIds(deleteId);

	}

	@Override
	public void unFreeByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] updateId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			updateId[i] = Long.valueOf(split[i]);
		}
		docDao.unFreeDoc(updateId);
	}

	@Override
	public void freeByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] updateId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			updateId[i] = Long.valueOf(split[i]);
		}
		docDao.freeDoc(updateId);
	}

	@Override
	public DocBean findWorkByEno(String eno)
	{
		DocBean docBean = null;
		try
		{
			Work work = workDao.getByEno(eno);
			if (work != null)
			{
				docBean = new DocBean();
				docBean.setAge(work.getAge());
				docBean.setD_name(work.getD_name());
				docBean.setId_card(work.getId_card());
				docBean.setSex(work.getSex());
				docBean.setPhone(work.getPhone());
				docBean.setEno(eno);
			}
		}
		catch (Exception e)
		{
			System.out.println("findWorkByEno error" + e);
		}
		return docBean;
	}

}
